package com.mall.app.web;


import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.mall.app.bean.TbCategory;
import com.mall.app.bean.TbGoods;
import com.mall.app.service.TbGoodsService;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author LeeSin
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/goods")
public class TbGoodsController {
	@Autowired
	private TbGoodsService goodsService;

	@RequestMapping("/listPage")
	public Map<String,Object> listPage(int page ,int limit,String gname,String cname){

		Map<String,Object>  map=new HashMap<String, Object>();
		map.put("page", (page-1)*limit);
		map.put("size", limit);
		map.put("cname", cname);
		map.put("gname", gname);
		Map<String,Object>  result=new HashMap<String, Object>();
		result.put("code", 0);
		result.put("msg", "");
		result.put("count", goodsService.getCount(map));
		result.put("data", goodsService.listPage(map));
		return result;
	}
	
	@RequestMapping("/delete")
	public boolean delete(@RequestParam("gids[]") String[] gids){
		return goodsService.deleteGoods(gids);
	}
	
	@RequestMapping("/update")
	public boolean update(TbGoods goods,int cid){
		LocalDateTime now = LocalDateTime.now();
		goods.setModifyTime(now);
		TbCategory category = new TbCategory();
		category.setCid(cid);
		goods.setCategory(category);
		return goodsService.updateGoods(goods);
	}
	
	@RequestMapping("/insert")
	public boolean insert(TbGoods goods,int cid,HttpSession session){
		String fileName = (String) session.getAttribute("fileName");
		if(fileName!=null) goods.setImages(fileName);
		LocalDateTime now = LocalDateTime.now();
		goods.setCreateTime(now);
		goods.setModifyTime(now);
		TbCategory category = new TbCategory();
		category.setCid(cid);
		goods.setCategory(category);
		return goodsService.insertGoods(goods);//转到业务层goodsService
	}
	
	@RequestMapping("/uploadImage")
	public boolean uploadImage(@RequestParam MultipartFile file,HttpSession session){
		String originalFilename = file.getOriginalFilename();
		if(originalFilename==null) return false;
		//获取扩展名
		String extension=originalFilename.substring(originalFilename.lastIndexOf("."));
		//生成随机名
		String fileName = UUID.randomUUID()+extension;
		session.setAttribute("fileName", fileName);
		//获取上传路径
		String filePath = session.getServletContext().getRealPath("/")+"res/goods/images/"+fileName;
		
		try {
			file.transferTo(new File(filePath));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	@RequestMapping("/list")
	public List<TbGoods> list(){
		return goodsService.listPage(null);
	}
	
	@RequestMapping("/getById")
	public TbGoods getById(int gid){
		Map<String,Object>  map=new HashMap<String, Object>();
		map.put("gid", gid);
		List<TbGoods> goodsList = goodsService.listPage(map);
		if(goodsList!=null && goodsList.size()>=1){
			return goodsList.get(0);
		}else{
			return new TbGoods();
		}
		
	}
	
}

